package com.xoa.dev.pz.service;


import com.xoa.model.enclosure.Attachment;

import com.xoa.model.users.Users;
import com.xoa.dev.pz.dao.PzHouseServiceMapper;
import com.xoa.dev.pz.model.PzHouseServiceWithBLOBs;
import com.xoa.service.users.UsersService;
import com.xoa.util.GetAttachmentListUtil;
import com.xoa.util.ToJson;
import com.xoa.util.common.L;
import com.xoa.util.common.StringUtils;
import com.xoa.util.common.session.SessionUtils;
import com.xoa.util.page.PageParams;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 创建作者:   牛江丽
 * 创建日期:   ${date} ${time}
 * 类介绍  :
 * 构造参数:
 */
@Service
public class PzHouseServerService {

    @Resource
    private PzHouseServiceMapper pzHouseServiceMapper;

    @Resource
    private UsersService usersService;

    /**
     * 创建作者:   牛江丽
     * 创建日期:   2017年11月21日 下午14:08:00
     * 方法介绍:   添加出租屋服务
     * 参数说明:   record
     * 返回值说明:
     */
    public ToJson<PzHouseServiceWithBLOBs> insertService(PzHouseServiceWithBLOBs record){
        ToJson<PzHouseServiceWithBLOBs> toJson = new ToJson<PzHouseServiceWithBLOBs>(1, "error");
        try {
            int count=pzHouseServiceMapper.insertService(record);
            if (count > 0) {
                toJson.setFlag(0);
                toJson.setMsg("ok");
            }
        } catch (Exception e) {
            toJson.setMsg(e.getMessage());
            L.e("PzHouseServiceController insertService:" + e);
        }
        return toJson;
    }

    /**
     * 创建作者:   牛江丽
     * 创建日期:   2017年11月21日 下午14:08:00
     * 方法介绍:   根据id进行修改出租屋服务信息
     * 参数说明:   record
     * 返回值说明:
     */
    public ToJson<PzHouseServiceWithBLOBs> upServiceById(PzHouseServiceWithBLOBs record){
        ToJson<PzHouseServiceWithBLOBs> toJson = new ToJson<PzHouseServiceWithBLOBs>(1, "error");
        try {
            int count=pzHouseServiceMapper.upServiceById(record);
            if (count > 0) {
                toJson.setFlag(0);
                toJson.setMsg("ok");
            }
        } catch (Exception e) {
            toJson.setMsg(e.getMessage());
            L.e("PzHouseServiceController upServiceById:" + e);
        }
        return toJson;
    }

    /**
     * 创建作者:   牛江丽
     * 创建日期:   2017年11月21日 下午14:08:00
     * 方法介绍:   根据id删除出租屋服务信息
     * 参数说明:   record
     * 返回值说明:
     */
    public ToJson<PzHouseServiceWithBLOBs> delServiceById(int serverId){
        ToJson<PzHouseServiceWithBLOBs> toJson = new ToJson<PzHouseServiceWithBLOBs>(1, "error");
        try {
            int count=pzHouseServiceMapper.delServiceById(serverId);
            if (count > 0) {
                toJson.setFlag(0);
                toJson.setMsg("ok");
            }
        } catch (Exception e) {
            toJson.setMsg(e.getMessage());
            L.e("PzHouseServiceController delServiceById:" + e);
        }
        return toJson;
    }

    /**
     * 创建作者:   牛江丽
     * 创建日期:   2017年11月21日 下午14:08:00
     * 方法介绍:   根据id查询出租屋服务信息
     * 参数说明:   serverId
     * 返回值说明:
     */
    public ToJson<PzHouseServiceWithBLOBs> selServiceById(int serverId, HttpServletRequest request){
        ToJson<PzHouseServiceWithBLOBs> toJson = new ToJson<PzHouseServiceWithBLOBs>(1, "error");
        try {
            PzHouseServiceWithBLOBs pzHouseServiceWithBLOBs=pzHouseServiceMapper.selServiceById(serverId);
            if(pzHouseServiceWithBLOBs!=null && !StringUtils.checkNull(pzHouseServiceWithBLOBs.getServerGridUser())){
                pzHouseServiceWithBLOBs.setUserName(usersService.getUserNameById(pzHouseServiceWithBLOBs.getServerGridUser()));
            }
            //附件处理
            List<Attachment> attachmentList=new ArrayList<Attachment>();
            if(pzHouseServiceWithBLOBs.getAttachmentName()!=null&&!"".equals(pzHouseServiceWithBLOBs.getAttachmentName())){
                attachmentList= GetAttachmentListUtil.returnAttachment(pzHouseServiceWithBLOBs.getAttachmentName(),pzHouseServiceWithBLOBs.getAttachmentId(),"xoa" + (String) request.getSession().getAttribute("loginDateSouse"), GetAttachmentListUtil.MODULE_PZ_HOUSESERVICE);
            }
            pzHouseServiceWithBLOBs.setAttachmentList(attachmentList);

            toJson.setObject(pzHouseServiceWithBLOBs);
            toJson.setFlag(0);
            toJson.setMsg("ok");
        } catch (Exception e) {
            toJson.setMsg(e.getMessage());
            L.e("PzHouseServiceController selServiceById:" + e);
        }
        return toJson;
    }

    /**
     * 创建作者:   牛江丽
     * 创建日期:   2017年11月21日 下午14:08:00
     * 方法介绍:   根据条件查询出租屋服务信息
     * 参数说明:   @param map
     * 返回值说明:
     */
    public ToJson<PzHouseServiceWithBLOBs> selServiceByCond(Integer page, Integer pageSize, boolean useFlag, PzHouseServiceWithBLOBs pzHouseServiceWithBLOBs, HttpServletRequest request){
        ToJson<PzHouseServiceWithBLOBs> toJson = new ToJson<PzHouseServiceWithBLOBs>(1, "error");
        try {
            //分页
            PageParams pageParams = new PageParams();
            pageParams.setPage(page);
            pageParams.setPageSize(pageSize);
            pageParams.setUseFlag(useFlag);
            Map<String, Object> map = new HashMap<String, Object>();
            map.put("page", pageParams);
            Users users= SessionUtils.getSessionInfo(request.getSession(),Users.class,new Users());
            pzHouseServiceWithBLOBs.setServerGridUser(users.getUserId());
            map.put("pzHouseServiceWithBLOBs",pzHouseServiceWithBLOBs);
            List<PzHouseServiceWithBLOBs> list=pzHouseServiceMapper.selServiceByCond(map);
            for(PzHouseServiceWithBLOBs server:list){
                if(server!=null && !StringUtils.checkNull(server.getServerGridUser())){
                    server.setUserName(usersService.getUserNameById(server.getServerGridUser()));
                }
//                if(server.getServerRecordType().equals("1")){
//                    server.setServerRecordType("排查类");
//                }
            }
            toJson.setTotleNum(pzHouseServiceMapper.selServiceCountByCond(pzHouseServiceWithBLOBs));
            toJson.setObj(list);
            toJson.setFlag(0);
            toJson.setMsg("ok");
        } catch (Exception e) {
            toJson.setMsg(e.getMessage());
            L.e("PzHouseServiceController selServiceByCond:" + e);
        }
        return toJson;
    }


}
